package priv.pfz.sbv;

import com.google.common.collect.Lists;
import lombok.Data;
import org.springframework.context.ApplicationContext;
import priv.pfz.sbv.spring.SbvClassPathXmlApplicationContext;

import java.util.List;

/**
 * @author yyjxp
 * 2024/2/16 2:38
 */
@Data
public class Visualizer {

    /**
     * spring配置路径
     */
    private String configLocation;

    /**
     * bean的分组规则
     * 由分组名称和beanName的正则表达式组成，例如Mapper .*Mapper.*
     */
    private List<BeanCategory> beanCategories = Lists.newArrayList(BeanCategory.DEFAULT_CATEGORY);

    /**
     * 生成页面的目标目录
     */
    private String htmlPath;

    /**
     * 是否排除spring内部bean
     */
    private boolean excludeSpringInnerBean = true;

    /**
     * 是否生成脱机页面
     */
    private boolean generateLocalHtml = false;

    /**
     * spring上下文环境
     */
    private ApplicationContext applicationContext;

    public static Visualizer newInstance() {
        return new Visualizer();
    }

    public Visualizer configLocation(String configLocation) {
        this.configLocation = configLocation;
        return this;
    }

    public Visualizer addBeanCategory(String categoryName, String beanNameRegex) {
        beanCategories.add(new BeanCategory(categoryName, beanNameRegex));
        return this;
    }

    public Visualizer htmlPath(String htmlPath) {
        this.htmlPath = htmlPath;
        return this;
    }

    public Visualizer excludeSpringInnerBean(boolean excludeSpringInnerBean) {
        this.excludeSpringInnerBean = excludeSpringInnerBean;
        return this;
    }

    public Visualizer generateLocalHtml(boolean generateLocalHtml) {
        this.generateLocalHtml = generateLocalHtml;
        return this;
    }

    public void visualize() {
        Utils.checkNotBlank(configLocation, "spring配置路径不能为空");
        Utils.checkNotBlank(htmlPath, "生成页面的目标目录不能为空");
        applicationContext = new SbvClassPathXmlApplicationContext(configLocation);

        //生成bean依赖关系
        DependenciesGenerator.generate(this);
        //生成bean初始化时间线
        InitTimelineGenerator.generate(this);
    }
}
